package com.qfedu.edu.result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
@Api("统一的结果集返回对象")
public class R {
    @ApiModelProperty("响应码")
    private Integer code;
    @ApiModelProperty("响应消息")
    private String message;
    @ApiModelProperty("响应数据")
    private Map<String,Object> data=new HashMap<>();
    private R(){
        this.code = ResponseCode.SUCCESS.getCode();
        this.message = ResponseCode.SUCCESS.getMessage();
    }
    public static R ok(){
        R r=new R();
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMessage(ResponseCode.SUCCESS.getMessage());
        return r;
    }
    public static R error(ResponseCode responseCode){
        R r=new R();
        r.setCode(responseCode.getCode());
        r.setMessage(responseCode.getMessage());
        return r;
    }
    public static R error(Integer code,String message){
        R r=new R();
        r.setCode(code);
        r.setMessage(message);
        return r;
    }
    public static  R error(){
        R r = new R();
        r.setCode(ResponseCode.FAIL.getCode());
        r.setMessage(ResponseCode.FAIL.getMessage());
        return  r;
    }
    public R data(String key,Object val){
        this.data.put(key,val);
        return this;
    }
    public R data(Map<String,Object> map){
       map.entrySet().forEach(entry->{
           this.data.put(entry.getKey(),entry.getValue());
       });
       return this;
    }
}
